// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Design/DDVALParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Input/Design/DDVALData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Design/IDDVALService.h"

#include "Import/BDFImport/BDFParserBase.h"

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class DDVALParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (DDVALParserTest, ParseEntry1)
{  //$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "DDVAL   20      2.      200." };

    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDDVAL (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::DDVALData data;

    DB_SERVICEFACTORY->get<IDDVALService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 20);
    ASSERT_TRUE (data.m_DVALList[0].m_V == 2.0);
    ASSERT_TRUE (data.m_DVALList[1].m_V == 200.0);
}

TEST_F (DDVALParserTest, ParseEntry2)
{
    std::vector<std::string> Entry = { "DDVAL,22,0.0,THRU,10000.0,BY,5000." };

    DB_SERVICEFACTORY->ClearDatabase ();
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDDVAL (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::DDVALData data;

    DB_SERVICEFACTORY->get<IDDVALService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 22);
    ASSERT_TRUE (data.m_DVALList[0].m_V == 0.0);
    ASSERT_TRUE (data.m_DVALList[1].m_V == 5000.0);
    ASSERT_TRUE (data.m_DVALList[2].m_V == 10000.0);
}

TEST_F (DDVALParserTest, ParseEntry3)
{
    std::vector<std::string> Entry = {
        "DDVAL   25      2.      200.",      // 父行：小字段格式
        "       ,0.0,THRU,10000.0,BY,5000."  // 子行：自由格式
    };

    DB_SERVICEFACTORY->ClearDatabase ();
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDDVAL (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::DDVALData data;

    DB_SERVICEFACTORY->get<IDDVALService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 25);
    ASSERT_TRUE (data.m_DVALList[0].m_V == 2.0);
    ASSERT_TRUE (data.m_DVALList[1].m_V == 200.0);
    ASSERT_TRUE (data.m_DVALList[2].m_V == 0.0);
    ASSERT_TRUE (data.m_DVALList[3].m_V == 5000.0);
    ASSERT_TRUE (data.m_DVALList[4].m_V == 10000.0);
}